<?php
/**
 * Created by PhpStorm.
 * User: wyqgg
 * Date: 2021/11/14
 * Time: 15:55
 */

class Medal extends Base_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->service('Medal_service');
        $this->load->model('Medal_model');
    }

    //上传勋章
    public function uploadMedal()
    {
        $data = $this->input->post();
        $rules = [
            [
                'field' => 'medal_name',
                'label' => '勋章名',
                'rules' => 'required|min_length[4]',
                'errors' => [
                    'required' => '勋章名不能为空',
                    'min_length' => '勋章名长度太短'
                ]
            ],
            [
                'field' => 'type',
                'label' => '勋章类型',
                'rules' => 'required|in_list[1,2,3]',
                'errors' => [
                    'required' => '勋章类型不能为空',
                    'in_list' => '勋章类型选择错误'
                ]
            ],
            [
                'field' => 'desc',
                'label' => '勋章描述',
                'rules' => 'required|min_length[4]',
                'errors' => [
                    'required' => '勋章描述不能为空',
                    'min_length' => '勋章描述太短'
                ]
            ],
        ];
        $checkParams = $this->validate($data, $rules);
        if ($checkParams !== true) {
            fail(400, $checkParams);
        }
        $data['image'] = imgUpload('image');
        $data['init_time'] = time();
        $res = $this->Medal_model->addMedal($data);
        if (!$res){
            fail(400,'操作失败');
        }
        //將所有勋章加载到redis中
        $this->setRedisMedal();
        success();
    }

    //删除勋章
    public function delMedal()
    {
        $data = $this->input->post();
        if (empty($data['id'])) {
            fail("勋章id不能为空!");
        }
        $res = $this->Medal_model->delMedal($data['id']);
        if (!$res) {
            fail(400,"删除勋章失败，请重试!");
        }
        $this->setRedisMedal();
        success("删除勋章成功!");
    }

    //修改勋章
    public function editMedal()
    {
        $data = $this->input->post();
        if (empty($data['id'])) {
            fail(400,'id不能为空');
        }
    }

    //查看全部勋章
    public function getAllMedal()
    {
        $data = $this->input->post();
        if(empty($data['uid'])){
            fail(400,'用户id不能为空!');
        }
        $redis = getRedis();
        $data = $redis->get(RedisKey::COMMON_MEDAL_GLOBAL_INFO);
        if (empty($data)){
            //数据库取值
            $data = $this->Medal_model->getAllMedal();
            if (!empty($data)){
                $redis = getRedis();
                $redis->set(RedisKey::COMMON_MEDAL_GLOBAL_INFO,$data);
            }else{
                success('','还没有一个勋章呢!');
            }
        }
        success($data,'获取勋章成功');
    }

    //设置勋章redis
    public function setRedisMedal()
    {
        //获取全部勋章
        $data = $this->Medal_model->getAllMedal();
        if (!empty($data)){
            $redis = getRedis();
            $redis->set(RedisKey::COMMON_MEDAL_GLOBAL_INFO,$data,86400*3);//三天过期
        }
    }
}